<?php
	namespace Home\Controller;

	class OrderController extends CommonController{
		public function Cart(){
			if(!empty($_POST)){
				if($_POST['num']<=0){
					$_POST['num']=1;
				}
				$list['num']=$_POST['num'];
			}
			$id=$_GET['id'];
			if(!empty($id)){	
				$model=D('Goods');
				$list=$model->getGoods($id);
				$list['giname']=$this->str($list['giname'],'80_');
				
			if(!empty($_SESSION['cart'][$id])){
					$_SESSION['cart'][$id]['num']=$_SESSION['cart'][$id]['num']+$_POST['num'];
			}else{
					$list['num']=$_POST['num'];
					$_SESSION['cart'][$id]=$list;
				 }
				
				$gk=$list['gk'];
				$gk=$gk-$_SESSION['cart'][$id]['num'];
				if($gk<0){
					$this->assign('库存不足');
					return false;
				}
				$da['stok']=$gk;
				$rese=$model->where("id=".$id)->save($da);
			}
			if($_SESSION['cart'][$id]['num']>$gk){
				$this->error('库存不足',U('Index/index'));
				return false;
			}
			
			foreach ($_SESSION['cart'] as $key => $value) {
				$total+=$value['gp']*$value['num'];
			}
			$this->assign('num',$total);
			$this->assign('list',$_SESSION['cart']);
			$this->assign('login',$_SESSION['home']);
			$this->display();
		}
		public function Order(){
			if(empty($_SESSION['cart'])){
				$this->error('请购物');
				return false;
			}
			$uid=$_SESSION['home']['id'];
			$address=M('Address');
			$data['uid']=$uid;
			$list=$address->where($data)->select();
			foreach ($_SESSION['cart'] as $key => $value) {
				$total+=$value['gp']*$value['num'];
			}
			$this->assign('data',$list);
			$this->assign('total',$total);
			$this->assign('list',$_SESSION['cart']);
			$this->assign('login',$_SESSION['home']);
			$this->display();

		}
		public function Address(){
			if(empty($_POST)){
				$this->assign('login',$_SESSION['home']);
				$this->display();
				return false;
			}		
			$data['address']=$_POST['address'];
			$data['status']=$_POST['status'];
			$data['uid']=$_SESSION['home']['id'];
			// $model=D('Address');
			// $res=$model->getAll($uid);
			$model=M('address');
			$res=$model->data($data)->add();
			if($res){
				$this->success('添加成功',U('Order/order'));
				return false;
			}else{
				$this->error('添加失败',U('Order/address'));
				return false;
			}

		}
		public function DoOrder(){
			$address=$_POST['address'];
			if(empty($address)){
				$this->error('请添加地址',U('Order/order'));
				return false;
			}
			$listmodel=M('Order_list');
			$listmodel->startTrans();
			$data['uid']=$_SESSION['home']['id'];
			$data['add_id']=$address;
			$data['ordernumber']='Mz2'.date('Ymd',time()).uniqid().rand(0,9999);
			$data['addtime']=time();
			$res=$listmodel->data($data)->add();
			if($res){
				$infomodel=M('Order_info');
				foreach($_SESSION['cart'] as $value) {
					$dat['order_id']=$res;
					$dat['goods_id']=$value['gid'];
					$dat['num']=$value['num'];
					$resu=$infomodel->data($dat)->add();
					$goodsmodel=M('Goods');
					$list=$goodsmodel->where($value['gid'])->find();
					
					if($resu){
						$listmodel->commit();
					}else{
						$listmodel->rollback();
						$this->error('结算出现问题');	
					}
				}		
			}else{
				$this->error('结算出现问题!');
			}
			
			unset($_SESSION['cart']);
			$this->success('购物成功',U('Order/orderend'),1);	
		}
		public function OrderEnd(){
			$this->assign('login',$_SESSION['home']);
			$this->display();
		}

		public function Order_info(){
			$id=$_SESSION['home']['id'];
			$ordermodel=M('Order_list');
			$total=$ordermodel->where("uid=".$id)->count();
			$page=new \Think\Page($total,4);
			$page->setConfig('prev','上一页');
			$page->setConfig('next','下一页');
			$page_str=$page->show();
			$limit=$page->firstRow.','.$page->listRows;
			$list=$ordermodel->field('ol.uid,u.name,u.id uid,ol.ordernumber olo,ol.is_fu,ol.is_show,add.address,ol.add_id,ol.id')->table('user u,order_list ol,`address` `add`')->where("u.id=ol.uid and ol.`add_id`=`add`.id and ol.uid=".$id)->limit($limit)->select();
			$this->assign('Page',$page_str);
			$this->assign('list',$list);
			$this->assign('login',$_SESSION['home']);
			$this->display();
		}
		public function doorder_isshow(){
			$id=$_GET['id'];
			$model=M('order_list');
			$data['is_show']=2;
			$res=$model->where("id=".$id)->save($data);
			if($res){
				$this->success('签收成功',U('Order/order_info'),2);
			}else{
				$this->error('付款失败');
			}
		}
		public function doorder_isfu(){
			$id=$_GET['id'];
			$model=M('order_list');
			$data['is_fu']=1;
			$res=$model->where("id=".$id)->save($data);
			if($res){
				$this->success('付款成功',U('Order/order_info'),2);
			}else{
				$this->error('付款失败');
			}
		}
		public function de(){
			$id=$_GET['id'];
			$listmodel=M('Order_list');
			$list=$listmodel->where("id=".$id)->find();
			if($list['is_fu']==1){
				if($list['is_show']!=2){
				$this->error('未收货不能删除');
				}
			}
			$listmodel->startTrans();
			$res=$listmodel->where("id=".$id)->delete();
			if($res){
				$infomodel=M('Order_info');
				$resu=$infomodel->where("order_id=".$id)->delete();
				if($resu){
					$listmodel->commit();
					$this->error('删除成功',U('Order/order_info'),1);
				}else{
					$listmodel->rollback();
					$this->error('删除失败');
				}
			}
		}

		public function Dele(){
			$id=$_GET['id'];
			$model=M('Address');
			$res=$model->where("id=".$id)->delete();
			if ($res) {
				$this->success('删除成功',U('Order/order'),2);
			}else{
				$this->error('删除失败',U('Order/order'),2);
			}
		}
		public function Del(){
			$id=$_GET['id'];
			unset($_SESSION['cart'][$id]); 
			$this->redirect('Order/cart');
		}
		public function Str($str,$zoom=''){
			$str='/Public/Uploads/'.substr($str,0,4).'/'.substr($str,4,2).'/'.substr($str,6,2).'/'.$zoom.$str;
			return $str;
   		}
	}